package com.cyt.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.cyt.entity.LunBoTu;
import com.cyt.entity.ShangPingXX;
import com.cyt.entity.SheJiShi;
import com.cyt.util.DBManager;
//轮播图信息返回集合
public class LunBoTuDaoImpl {
	/**
	 * 查轮播图所有
	 * @return
	 */
	public List<LunBoTu> getLunBoTu() {
		List<LunBoTu> list = new ArrayList<LunBoTu>();
		Statement st = null;
		ResultSet rs = null;
		// 1.建立连接
		Connection conn = DBManager.getConnection();
		// 2.创建Statement执行对象(用来执行sql语句)
		try {// try 正常执行
			st = conn.createStatement();
			// 3.sql语句
			String sql = "SELECT * FROM lunbotu";
			// 4.执行sql语句,返回结果集
			rs = st.executeQuery(sql);
			// 5. 遍历结果集
			while (rs.next()) { 
				int lunBoId = rs.getInt("lunBoId");
				String lunBoImg = rs.getString("lunBoImg");
				LunBoTu lun = new LunBoTu(lunBoId, lunBoImg);// 创建用户对象
				list.add(lun);// 把user对象存入list中
			}
			return list;// 正常情况返回集合
         
		} catch (SQLException e) {// catch 异常执行
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;// 异常返回空
		} finally {
			DBManager.closeAll(conn, st, rs);
		}

	}
	
	/**
	 * 分页
	 * @return
	 */
	public List<LunBoTu> getLunBoTuList(int page, int size) {
		List<LunBoTu> list = new ArrayList<LunBoTu>();
		Statement st = null;
		ResultSet rs = null;
		// 1.建立连接
		Connection conn = DBManager.getConnection();
		// 2.创建Statement执行对象(用来执行sql语句)
		try {// try 正常执行
			st = conn.createStatement();
			// 3.sql语句
			String sql = "SELECT * FROM lunbotu LIMIT "+(page-1)*size+","+size;
			// 4.执行sql语句,返回结果集
			rs = st.executeQuery(sql);
			// 5. 遍历结果集
			while (rs.next()) { 
				int lunBoId = rs.getInt("lunBoId");
				String lunBoImg = rs.getString("lunBoImg");
				LunBoTu lun = new LunBoTu(lunBoId, lunBoImg);// 创建用户对象
				list.add(lun);// 把user对象存入list中
			}
			return list;// 正常情况返回集合
         
		} catch (SQLException e) {// catch 异常执行
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;// 异常返回空
		} finally {
			DBManager.closeAll(conn, st, rs);
		}

	}
	
	/**
	 * 根据id删除轮播图 
	 * @param lunId要删除的轮播图的id
	 * @return返回数据库受影响行数
	 */
	public int shanChuLunBoTuFL(int lunId) {
	 return DBManager.executeUpdate("DELETE FROM lunbotu where lunBoId="+lunId);
	
	
	}
	
	/**
	 * 通过id主键删除
     * 批量删除
	 * @param 
	 * @return
	 */
	public int shanLunBoTuFLS(String ids) {
		String sql = "delete from lunbotu where lunBoId in("+ids+")";
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	
	
	/**
	 * 添加轮播图
	 * @param lbt
	 * @return
	 */
	public int addLunBoTu(LunBoTu lbt ) {
		return DBManager.executeUpdate("INSERT into lunbotu VALUES(null,'"+lbt.getLunBoImg()+"')");
	}
	
	
	/**
	 * 根据id查轮播图信息,以供修改的数据回显
	 * @param lunId
	 * @return
	 */
	public LunBoTu chaLunBoTuId(int lunId) {
		LunBoTu lunBo=null;
		Statement st = null;
		ResultSet rs = null;
		// 1.建立连接
		Connection conn = DBManager.getConnection();
		// 2.创建Statement执行对象(用来执行sql语句)
		try {// try 正常执行
			st = conn.createStatement();
			String sql = "SELECT * FROM lunbotu WHERE lunBoId="+lunId;
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int lunBoId = rs.getInt("lunBoId");
				String lunBoImg = rs.getString("lunBoImg");
			lunBo = new LunBoTu(lunBoId, lunBoImg);
			}
			return lunBo;

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}

	}
	
	/**
	 * 修改设计师详细  根据id
	 * @param lun
	 * @return
	 */
	public int xiuGaiLunBoTu(LunBoTu lun ) {
		String sql="UPDATE lunbotu set lunBoImg='"+lun.getLunBoImg()+"' WHERE lunBoId="+lun.getLunBoId();
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	
}
